/*******************************************************************************
****文件路径         : /vtk_demo/src/test.cpp
****作者名称         : shibiaoshen
****文件版本         : V1.0.0
****创建日期         : 2025-05-14 02:53:13
****简要说明         : 
****
****版权信息         : 2025 by ${git_name}, All Rights Reserved.
********************************************************************************/

#include "test.h"



void test_cylinder(){
    vtkSmartPointer<vtkCylinderSource>cylinder=vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetHeight(3.0);
    cylinder->SetRadius(1.0);
    cylinder->SetResolution(10);

    vtkSmartPointer<vtkPolyDataMapper>mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(cylinder->GetOutputPort());


    vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer>render=vtkSmartPointer<vtkRenderer>::New();
    render->AddActor(actor);
    render->SetBackground(0.1,0.2,0.4);

    vtkSmartPointer<vtkLight>mylight=vtkSmartPointer<vtkLight>::New();
    mylight->SetColor(0,1,0);
    mylight->SetPosition(0,0,1);
    mylight->SetFocalPoint(render->GetActiveCamera()->GetFocalPoint());
    render->AddLight(mylight);


    vtkSmartPointer<vtkLight>mylight_2=vtkSmartPointer<vtkLight>::New();
    mylight_2->SetColor(0,0,1);
    mylight_2->SetPosition(0,0,-1);
    mylight_2->SetFocalPoint(render->GetActiveCamera()->GetFocalPoint());
    render->AddLight(mylight_2);

    vtkSmartPointer<vtkCamera> mycamera=vtkSmartPointer<vtkCamera>::New();
    mycamera->SetClippingRange(0.0475,2.3786);
    mycamera->SetFocalPoint(0.0573,-0.2134,-0.0523);
    mycamera->SetPosition(0.3245,-0.1139,-0.2932);
    mycamera->SetViewUp(-0.2234,0.9983,0.0345);
    render->SetActiveCamera(mycamera);
    vtkSmartPointer<vtkRenderWindow>win=vtkSmartPointer<vtkRenderWindow>::New();
    win->AddRenderer(render);
    win->SetSize(600,600);
    vtkSmartPointer<vtkRenderWindowInteractor>it=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    it->SetRenderWindow(win);
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>style=vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    it->SetInteractorStyle(style);
    it->Initialize();
    std::cout<<"exec"<<endl;
    it->Start();

    
}


bool polyDataSave(){
    vtkSmartPointer<vtkPoints>points=vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(1.0,0.0,0.0);
    points->InsertNextPoint(0.0,0.0,0.0);
    points->InsertNextPoint(0.0,1.0,0.0);
    vtkSmartPointer<vtkPolyData>polyData=vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points);

    vtkSmartPointer<vtkPolyDataWriter>writer=vtkSmartPointer<vtkPolyDataWriter>::New();
    writer->SetFileName("triangle.vtk");
    writer->SetInputData(polyData);
    writer->Write();

}


bool topologyDataSave(std::string path){

    double X[3]={1.0,0.0,0.0};
    double Y[3]={0.0,0.0,1.0};
    double Z[3]={0.0,0.0,0.0};

    vtkSmartPointer<vtkPoints>points=vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray>vertices=vtkSmartPointer<vtkCellArray>::New();

    for(int i=0;i<3;i++){
        vtkIdType pid[1];
        pid[0]=points->InsertNextPoint(X[i],Y[i],Z[i]);
        vertices->InsertNextCell(1,pid);
    }

    vtkSmartPointer<vtkPolyData>polyData=vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points);
    polyData->SetVerts(vertices);


    vtkSmartPointer<vtkPolyDataWriter>writer=vtkSmartPointer<vtkPolyDataWriter>::New();
    writer->SetFileName("../data/TriangleVertices.vtk");
    writer->SetInputData(polyData);
    writer->Write();


}

void painScalar(){
    vtkSmartPointer<vtkPoints>points=vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(0,0,0);
    points->InsertNextPoint(1,0,0);

    //创建多边形数据
    vtkSmartPointer<vtkPolyData>polyData=vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points);


   vtkSmartPointer<vtkDoubleArray> weights = vtkSmartPointer<vtkDoubleArray>::New();
    weights->SetNumberOfValues(2);       // 旧方式（仍可用）
    weights->SetValue(0,1);
    weights->SetValue(1,2);

    polyData->GetPointData()->SetScalars(weights);

    //输出索引号为0的标量值
    double weight=vtkDoubleArray::SafeDownCast(polyData->GetPointData()->GetScalars())->GetValue(0);
    std::cout<<"double weight:"<<weight<<std::endl;

}


bool imageWrite(std::string path){

    vtkSmartPointer<vtkPNGReader>reader=vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName(path.c_str());
    vtkSmartPointer<vtkImageViewer2>image_viewer=vtkSmartPointer<vtkImageViewer2>::New();
    image_viewer->SetInputConnection(reader->GetOutputPort());
    vtkSmartPointer<vtkRenderWindowInteractor>interactor=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    image_viewer->SetupInteractor(interactor);
    image_viewer->Render();
    image_viewer->GetRenderer()->ResetCamera();
    image_viewer->Render();


    interactor->Start();
}

int vtkShowImage(std::string&path){
    vtkSmartPointer<vtkMetaImageReader>reader=vtkSmartPointer<vtkMetaImageReader>::New();
    reader->SetFileName(path.c_str());
    reader->Update();

    vtkSmartPointer<vtkImageViewer2>image_viewer=vtkSmartPointer<vtkImageViewer2>::New();
    vtkSmartPointer<vtkRenderWindowInteractor>interactor=vtkSmartPointer<vtkRenderWindowInteractor>::New();

    image_viewer->SetInputConnection(reader->GetOutputPort());
    image_viewer->SetupInteractor(interactor);
    image_viewer->SetColorLevel(500);
    image_viewer->SetColorWindow(2000);
    image_viewer->SetSlice(40);
    image_viewer->SetSliceOrientationToXY();
    image_viewer->Render();


    interactor->Start();

}


void showImageActor(std::string&filename){

    vtkSmartPointer<vtkPNGReader>reader=vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName(filename.c_str());
    reader->Update();
    vtkSmartPointer<vtkImageActor>imgActor=vtkSmartPointer<vtkImageActor>::New();
    imgActor->SetInputData(reader->GetOutput());

    vtkSmartPointer<vtkRenderer>render=vtkSmartPointer<vtkRenderer>::New();
    render->AddActor(imgActor);
    render->SetBackground(0.4,0.5,0.6);


    vtkSmartPointer<vtkRenderWindow>win=vtkSmartPointer<vtkRenderWindow>::New();
    win->SetSize(50,500);
    win->AddRenderer(render);


    vtkSmartPointer<vtkRenderWindowInteractor>interactor=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage>style=vtkSmartPointer<vtkInteractorStyleImage>::New();


    interactor->SetInteractorStyle(style);
    interactor->SetRenderWindow(win);
    interactor->Initialize();
    interactor->Start();



}